


<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->

<!-- Mirrored from www.cyclismo.org/tutorial/matlab/dataFile.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 04:32:29 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8" /><!-- /Added by HTTrack -->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>Data Files &mdash; Matlab Tutorial 3.0 documentation</title>
  

  
  

  

  
  
    

  

  
  
    <link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
  

  
    <link rel="stylesheet" href="_static/siteColours.css" type="text/css" />
  

  
    <link rel="top" title="Matlab Tutorial 3.0 documentation" href="index.html"/>
        <link rel="prev" title="The If Statement" href="if.html"/> 

  
  <script src="_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav" role="document">

  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="index.html" class="icon icon-home"> Matlab Tutorial
          

          
          </a>

          
            
            
              <div class="version">
                3.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="https://www.cyclismo.org/tutorial/matlab/search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          

<div class="local-toc"><ul>
<li><a class="reference internal" href="#">Data Files</a><ul>
<li><a class="reference internal" href="#saving-and-recalling-data">Saving and Recalling Data</a></li>
<li><a class="reference internal" href="#saving-a-session-as-text">Saving a Session as Text</a></li>
<li><a class="reference internal" href="#c-style-read-write">C Style Read/Write</a></li>
</ul>
</li>
</ul>
</div>
					


<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="vector.html">Introduction to Vectors in Matlab</a></li>
<li class="toctree-l1"><a class="reference internal" href="matrix.html">Introduction to Matrices in Matlab</a></li>
<li class="toctree-l1"><a class="reference internal" href="operations.html">Vector Functions</a></li>
<li class="toctree-l1"><a class="reference internal" href="control.html">Loops</a></li>
<li class="toctree-l1"><a class="reference internal" href="plot.html">Plotting</a></li>
<li class="toctree-l1"><a class="reference internal" href="execute.html">Executable Files</a></li>
<li class="toctree-l1"><a class="reference internal" href="subroutine.html">Subroutines</a></li>
<li class="toctree-l1"><a class="reference internal" href="if.html">The If Statement</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Data Files</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#saving-and-recalling-data">Saving and Recalling Data</a></li>
<li class="toctree-l2"><a class="reference internal" href="#saving-a-session-as-text">Saving a Session as Text</a></li>
<li class="toctree-l2"><a class="reference internal" href="#c-style-read-write">C Style Read/Write</a></li>
</ul>
</li>
</ul>


<div class="datacampAd">
  <span style="vertical-align:top;color: black;">
<center>
<script type="text/javascript"><!--
google_ad_client = "ca-pub-8301099787686694";
google_ad_width = 120;
google_ad_height = 240;
google_ad_type = "text_image";
google_ad_channel ="6171035172";
google_ad_slot = "4696657180";
google_color_border = "808080";
google_color_bg = "F0ECEC";
google_color_link = "000000";
google_color_url = "666666.html";
google_color_text = "000000";
//--></script>
<script type="text/javascript"
  src="../../../pagead2.googlesyndication.com/pagead/f.txt">
</script>
</center>
</span>
</div>


        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
        <a href="index.html">Matlab Tutorial</a>
      </nav>


      
      <div class="wy-nav-content">
        <div class="rst-content">
          





<div role="navigation" aria-label="breadcrumbs navigation">
  <ul class="wy-breadcrumbs">
    <li><a href="index.html">Docs</a> &raquo;</li>
      
    <li>Data Files</li>
      <li class="wy-breadcrumbs-aside">
        
          
        
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="data-files">
<h1>Data Files<a class="headerlink" href="#data-files" title="Permalink to this headline">¶</a></h1>
<p>Matlab does not allow you to save the commands that you have entered
in a session, but it does allow a number of different ways to save the
data. In this tutorial we explore the different ways that you can save
and read data into a Matlab session.</p>
<ol class="arabic simple">
<li><a class="reference internal" href="#savingdata"><span>Saving and Recalling Data</span></a></li>
<li><a class="reference internal" href="#savingsession"><span>Saving a Session as Text</span></a></li>
<li><a class="reference internal" href="#cstylewrite"><span>C Style Read/Write</span></a></li>
</ol>
<div class="section" id="saving-and-recalling-data">
<span id="savingdata"></span><h2>Saving and Recalling Data<a class="headerlink" href="#saving-and-recalling-data" title="Permalink to this headline">¶</a></h2>
<p>As you work through a session you generate vectors and matrices. The
next question is how do you save your work? Here we focus on how to
save and recall data from a Matlab session. The command to save
<strong>all</strong> of the data in a session is <em>save</em>. The command to bring the
data set in a data file back into a session is <em>load</em>.</p>
<p>We first look at the save command. In the example below we use the
most basic form which will save all of the data present in a session.
Here we save all of the data in a file called &#8220;stuff.mat.&#8221; (.mat is
the default extension for Matlab <strong>data</strong>.)</p>
<span class="target" id="index-0"></span><div class="highlight-matlab" id="index-1"><div class="highlight"><pre><span class="o">&gt;&gt;</span> <span class="n">u</span> <span class="p">=</span> <span class="p">[</span><span class="mi">1</span> <span class="mi">3</span> <span class="o">-</span><span class="mi">4</span><span class="p">];</span>
<span class="o">&gt;&gt;</span> <span class="n">v</span> <span class="p">=</span> <span class="p">[</span><span class="mi">2</span> <span class="o">-</span><span class="mi">1</span> <span class="mi">7</span><span class="p">];</span>
<span class="o">&gt;&gt;</span> <span class="n">whos</span>
  <span class="n">Name</span>      <span class="n">Size</span>                    <span class="n">Bytes</span>  <span class="n">Class</span>

  <span class="n">u</span>         <span class="mi">1</span><span class="n">x3</span>                        <span class="mi">24</span>  <span class="n">double</span> <span class="n">array</span>
  <span class="n">v</span>         <span class="mi">1</span><span class="n">x3</span>                        <span class="mi">24</span>  <span class="n">double</span> <span class="n">array</span>

<span class="n">Grand</span> <span class="n">total</span> <span class="n">is</span> <span class="mi">6</span> <span class="n">elements</span> <span class="n">using</span> <span class="mi">48</span> <span class="n">bytes</span>

<span class="o">&gt;&gt;</span> <span class="n">save</span> <span class="n">stuff</span><span class="p">.</span><span class="n">mat</span>
<span class="o">&gt;&gt;</span> <span class="n">ls</span>
<span class="n">stuff</span><span class="p">.</span><span class="n">mat</span>
</pre></div>
</div>
<p>The <em>ls</em> command is used to list all of the files in the current
directory. In this situation we created a file called &#8220;stuff.mat&#8221;
which contains the vectors <em>u</em> and <em>v</em>. The data can be read back in
to a Matlab session with the load command.</p>
<div class="highlight-matlab" id="index-2"><div class="highlight"><pre><span class="o">&gt;&gt;</span> <span class="n">clear</span>
<span class="o">&gt;&gt;</span> <span class="n">whos</span>
<span class="o">&gt;&gt;</span> <span class="n">load</span> <span class="n">stuff</span><span class="p">.</span><span class="n">mat</span>
<span class="o">&gt;&gt;</span> <span class="n">whos</span>
  <span class="n">Name</span>      <span class="n">Size</span>                    <span class="n">Bytes</span>  <span class="n">Class</span>

  <span class="n">u</span>         <span class="mi">1</span><span class="n">x3</span>                        <span class="mi">24</span>  <span class="n">double</span> <span class="n">array</span>
  <span class="n">v</span>         <span class="mi">1</span><span class="n">x3</span>                        <span class="mi">24</span>  <span class="n">double</span> <span class="n">array</span>

<span class="n">Grand</span> <span class="n">total</span> <span class="n">is</span> <span class="mi">6</span> <span class="n">elements</span> <span class="n">using</span> <span class="mi">48</span> <span class="n">bytes</span>

<span class="o">&gt;&gt;</span> <span class="n">u</span><span class="o">+</span><span class="n">v</span>

<span class="nb">ans</span> <span class="p">=</span>

     <span class="mi">3</span>     <span class="mi">2</span>     <span class="mi">3</span>
</pre></div>
</div>
<p>In this example the current data space is cleared of all variables.
The contents of the entire data file, &#8220;stuff.mat,&#8221; is then read back
into memory. You do not have to load all of the contents of the file
into memory. After you specify the file name you then list the
variables that you want to load separated by spaces. In the following
example only the variable u will be loaded into memory.</p>
<div class="highlight-matlab"><div class="highlight"><pre><span class="o">&gt;&gt;</span> <span class="n">clear</span>
<span class="o">&gt;&gt;</span> <span class="n">whos</span>
<span class="o">&gt;&gt;</span> <span class="n">load</span> <span class="n">stuff</span><span class="p">.</span><span class="n">mat</span> <span class="n">u</span>
<span class="o">&gt;&gt;</span> <span class="n">whos</span>
  <span class="n">Name</span>      <span class="n">Size</span>                    <span class="n">Bytes</span>  <span class="n">Class</span>

  <span class="n">u</span>         <span class="mi">1</span><span class="n">x3</span>                        <span class="mi">24</span>  <span class="n">double</span> <span class="n">array</span>

<span class="n">Grand</span> <span class="n">total</span> <span class="n">is</span> <span class="mi">3</span> <span class="n">elements</span> <span class="n">using</span> <span class="mi">24</span> <span class="n">bytes</span>

<span class="o">&gt;&gt;</span> <span class="n">u</span>

<span class="n">u</span> <span class="p">=</span>

     <span class="mi">1</span>     <span class="mi">3</span>    <span class="o">-</span><span class="mi">4</span>
</pre></div>
</div>
<p>Note that the save command works in exactly the same way. If you only
want to save a couple of variables you list the variables you want to
save after the file name. Again, the variables must be separated by a
space. For an example and more details please see the help file for
<em>save</em>. When in matlab just type in help save to see more information.
You will find that there are large number of options in terms of how
the data can be saved and the format of the data file.</p>
</div>
<div class="section" id="saving-a-session-as-text">
<span id="savingsession"></span><h2>Saving a Session as Text<a class="headerlink" href="#saving-a-session-as-text" title="Permalink to this headline">¶</a></h2>
<p>Matlab allows you to save the data generated in a session, but you
cannot easily save the commands so that they can be used in an
executable file (<a class="reference internal" href="execute.html#executablefile"><span>Executable Files</span></a>). You can save a copy of what
happened in a session using the diary command. This is very useful if
you want to save a session for a homework assignment or as a way to
take notes.</p>
<p>A diary of a session is initiated with the diary command followed by
the file name that you want to keep the text file. You then type in
all of the necessary commands. When you are done enter the diary
command alone, and it will write all of the output to the file and
close the file. In the example below a file called &#8220;save.txt&#8221; is
created that will contain a copy of the session.</p>
<div class="highlight-matlab" id="index-3"><div class="highlight"><pre><span class="o">&gt;&gt;</span> <span class="n">diary</span> <span class="n">save</span><span class="p">.</span><span class="n">txt</span>
        <span class="c">... enter commands here...</span>
<span class="o">&gt;&gt;</span> <span class="n">diary</span>
</pre></div>
</div>
<p>This will create a file called &#8220;save.txt&#8221; which will hold an exact
copy of the output from your session. This is how I generated the
files used in these tutorials.</p>
</div>
<div class="section" id="c-style-read-write">
<span id="cstylewrite"></span><h2>C Style Read/Write<a class="headerlink" href="#c-style-read-write" title="Permalink to this headline">¶</a></h2>
<p id="index-4">In addition to the high level read/write commands detailed above,
Matlab allows C style file access. This is extremely helpful since the
output generated by many home grown programs is in binary format due
to disk space considerations. This is an advanced subject, and we do
not go into great detail here. Instead we look at the basic commands.
After looking at this overview we highly recommend that you look
through the relevant help files. This will help fill in the missing
blanks.</p>
<p>The basic idea is that you open a file, execute the relevant reads and
writes on a file, and then close a file. One other common task is to
move the file pointer to point to a particular place in the file, and
there are two commands, fseek and ftell to help.</p>
<p>Here we give a very simple example. In the example, a file called
&#8220;laser.dat&#8221; is opened. The file identifier is kept track of using a
variable called <em>fp</em>. Once the file is opened the file position is
moved to a particular place in the file, denoted pos , and two double
precision numbers are read. Once that is done the position within the
file is stored, and the file is closed.</p>
<span class="target" id="index-5"></span><span class="target" id="index-6"></span><span class="target" id="index-7"></span><span class="target" id="index-8"></span><div class="highlight-matlab" id="index-9"><div class="highlight"><pre><span class="n">fp</span> <span class="p">=</span> <span class="n">fopen</span><span class="p">(</span><span class="s">&#39;laser.dat&#39;</span><span class="p">,</span><span class="s">&#39;r&#39;</span><span class="p">);</span>
<span class="n">fseek</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span><span class="n">pos</span><span class="p">,</span><span class="s">&#39;bof&#39;</span><span class="p">);</span>
<span class="n">tmp</span> <span class="p">=</span> <span class="n">fread</span><span class="p">(</span><span class="n">fp</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="s">&#39;double&#39;</span><span class="p">);</span>
<span class="n">pos</span> <span class="p">=</span> <span class="n">ftell</span><span class="p">(</span><span class="n">fp</span><span class="p">);</span>
<span class="n">fclose</span><span class="p">(</span><span class="n">fp</span><span class="p">);</span>
</pre></div>
</div>
</div>
</div>


           </div>
          </div>
          



  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
      
        <a href="if.html" class="btn btn-neutral" title="The If Statement" accesskey="p"><span class="fa fa-arrow-circle-left"></span> Previous</a>
      
    </div>
		

		<br><br>


<footer>
		<hr/>

		<div role="contentinfo">
          <center>
          <script async src="../../../pagead2.googlesyndication.com/pagead/js/f.txt"></script>
<!-- matlab tutorial wide -->
<ins class="adsbygoogle"
     style="display:inline-block;width:728px;height:90px"
     data-ad-client="ca-pub-8301099787686694"
     data-ad-slot="8763269985"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</center>


			<p>
			<span xmlns:dct="http://purl.org/dc/terms/" href="http://purl.org/dc/dcmitype/Text" property="dct:title" rel="dct:type">Matlab Tutorial</span> by <a xmlns:cc="http://creativecommons.org/ns#" href="mailto:kjblack@gmail.com" property="cc:attributionName" rel="cc:attributionURL">Kelly Black</a> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/">Creative Commons Attribution-NonCommercial 4.0 International License</a> (2015).<br />Based on a work at <a xmlns:dct="http://purl.org/dc/terms/" href="index-2.html" rel="dct:source">http://www.cyclismo.org/tutorial/matlab/</a>.
            </p>

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org/">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'./',
            VERSION:'3.0',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true
        };
    </script>
      <script type="text/javascript" src="_static/jquery.js"></script>
      <script type="text/javascript" src="_static/underscore.js"></script>
      <script type="text/javascript" src="_static/doctools.js"></script>
      <script type="text/javascript" src="../../../cdn.mathjax.org/mathjax/latest/MathJaxdda6.js?config=TeX-AMS-MML_HTMLorMML"></script>

  

  
  
    <script type="text/javascript" src="_static/js/theme.js"></script>
  

  
  
  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.StickyNav.enable();
      });
  </script>
  
<p>
<a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/"><img alt="Creative Commons License" style="border-width:0" src="../../../licensebuttons.net/l/by-nc/4.0/80x15.png" /></a><br /><span xmlns:dct="http://purl.org/dc/terms/" href="http://purl.org/dc/dcmitype/Text" property="dct:title" rel="dct:type">Matlab Tutorial</span> by <a xmlns:cc="http://creativecommons.org/ns#" href=href%3d_mailto_kjblack%40gmail.html property="cc:attributionName" rel="cc:attributionURL">Kelly Black</a> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/">Creative Commons Attribution-NonCommercial 4.0 International License</a>.<br />Based on a work at <a xmlns:dct="http://purl.org/dc/terms/" href="index-2.html" rel="dct:source">http://www.cyclismo.org/tutorial/matlab/</a>.
</p>
<p>This page generated using <a href="http://sphinx-doc.org/">Sphinx</a>.</p>





</body>

<!-- Mirrored from www.cyclismo.org/tutorial/matlab/dataFile.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 04:32:29 GMT -->
</html>